package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.web.controller.DepartmentController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.*;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(permissionMapper.selectForList(qo));
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        List<Permission> permissionList = permissionMapper.selectAll();
        Set<String> expressionList = new HashSet<>();
        for (Permission permission : permissionList) {
            expressionList.add(permission.getExpression());
        }
        // 获取容器对象并获取贴有@Controller注解的bean
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> collection = beans.values();
        for (Object controller : collection) {
            for (Method method : controller.getClass().getDeclaredMethods()) {
                // 获取方法里面的的注解,判断是否贴有自定义的注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if (annotation != null){
                    // 判断数据库中是否有这个注解表达式
                    String name = annotation.name();
                    String expression = annotation.expression();
                    if (!expressionList.contains(expression)){
                        // 如果没有,则创建 Permission 对象,封装数据并存入数据库
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }


    }

    @Override
    public List<String> queryByEmployeeId(Long employeeId) {
        return permissionMapper.selectByEmployeeId(employeeId);
    }

}
